/**
 * 通用获取详情
 * @url admin/system/base/getDetail
 * @description 通用获取详情操作
 */
module.exports = {
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '' };
		
		// 表名映射配置
		const dbNameMap = {
			user: 'uni-id-users',
			role: 'uni-id-roles',
			permission: 'uni-id-permissions',
			menu: 'opendb-admin-menus'
		};
		
		// 业务逻辑开始-----------------------------------------------------------
		let { type, _id } = data;
		
		if (!type || !dbNameMap[type]) {
			return { code: -1, msg: '类型参数错误' };
		}
		
		if (!_id) {
			return { code: -1, msg: 'ID不能为空' };
		}
		
		let dbName = dbNameMap[type];
		
		// 字段筛选（根据类型）
		let query = db.collection(dbName).doc(_id);
		if (type === 'user') {
			query = query.field('username,nickname,mobile,email,role,status,avatar,dcloud_appid,permission');
		} else if (type === 'role') {
			query = query.field('role_id,role_name,permission,comment,create_date');
		} else if (type === 'permission') {
			query = query.field('permission_id,permission_name,comment,create_date');
		} else if (type === 'menu') {
			query = query.field('menu_id,name,icon,url,parent_id,sort,enable,create_date');
		}
		
		// 查询详情
		let detailRes = await query.get();
		
		if (detailRes.data.length === 0) {
			return { code: -1, msg: '数据不存在' };
		}
		
		res.data = detailRes.data[0];
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}

